package com.example.demo.controller;

import com.example.demo.pojo.User;
import com.example.demo.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 翁佃峰
 */
@RestController
@RequestMapping("/users")
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 测试
     *
     * @return
     */
    @GetMapping("test")
    public String test() {
        return "my first API";
    }

    /**
     * 获取用户信息列表
     *
     * @return
     */
    @GetMapping("getUserList")
    public List<User> getUserList() {
        return userService.getUserList();
    }

    /**
     * 添加用户
     *
     * @param user
     */
    @PostMapping("addUser")
    public void addUser(@RequestBody User user) {
        userService.addUser(user);
    }

    /**
     * 更新用户
     *
     * @param user
     */
    @PutMapping("updateUser")
    public void updateUser(@RequestBody User user) {
        userService.updateUser(user);
    }

    /**
     * 删除用户
     *
     * @param id
     */
    @PostMapping("deleteUser")
    public void deleteUserById(String id) {
        userService.deleteUserById(id);
    }

    /**
     * 添加多个用户
     *
     * @param users
     */
    @PostMapping("addMultipleUsers")
    public void addMultipleUsers(@RequestBody List<User> users) {
        userService.addMultipleUsers(users);
    }

    /**
     * 删除多个用户
     *
     * @param id
     */
    @PostMapping("deleteMultipleUsers")
    public void deleteMultipleUsers(@RequestParam("id") String[] id) {
        userService.deleteMultipleUsers(id);
    }

    /**
     * 根据用户编号得到多个用户信息
     *
     * @param id
     */
    @PostMapping("getMultipleUsersById")
    public List<User> getMultupleUsersById(@RequestParam("id") String[] id) {
        return userService.getMultipleUsersById(id);
    }

    /**
     * 根据所给条件筛选用户
     * @param user
     * @return
     */
    @PostMapping("getUserByCondition")
    public List<User> getUserByCondition(@RequestBody User user) {
        return userService.getUserByCondition(user);
    }
    /**
     * 根据所给属性更新用户信息
     * @param user
     */
    @PostMapping("setUserByCondition")
    public void setUserByCondition(@RequestBody User user){
        userService.setUserByCondition(user);
    }
}